#============================================================
#	Fold Button
#============================================================
#  折叠按钮，点击弹出折叠的节点
#============================================================
# @datetime: 2022-5-14 15:47:31
#============================================================
tool
extends ToolButton


onready var _dialog = get_node("Dialog") 
onready var _margin_container = get_node("Dialog/MarginContainer/ScrollContainer/MarginContainer") 
onready var _scroll_container = get_node("Dialog/MarginContainer/ScrollContainer")


var _last_parent: Node
var _panel : Control


#============================================================
#   Set/Get
#============================================================
##  设置显示的 Panel 节点
## @node  
func set_panel(node: Control):
	if not self.is_inside_tree():
		yield(self, "ready")
	_panel = node
	if node.is_inside_tree():
		_last_parent = _panel.get_parent()
		_last_parent.remove_child(_panel)
		if _last_parent.get_child_count() == 0:
			_last_parent.hide()
			if _last_parent.get_parent() is HSplitContainer:
				_last_parent.get_parent().collapsed = true
	else:
		_last_parent = null
	
	_panel.visible = true
	if not _panel.is_inside_tree():
		_margin_container.add_child(_panel)
	_dialog.window_title = node.name


#============================================================
#   自定义
#============================================================
##  移除 Panel 
func remove_panel():
	_panel.get_parent().remove_child(_panel)
	if _last_parent:
		_last_parent.add_child(_panel)
		_last_parent.show()
		if _last_parent.get_parent() is HSplitContainer:
			_last_parent.get_parent().collapsed = false


##  显示 Panel 
func show_panel():
	if not _dialog.visible:
		_dialog.rect_position.x = self.rect_size.x
		_dialog.popup()


##  隐藏 Panel 
func hide_panel():
	if _dialog.visible:
		_dialog.hide()


#============================================================
#   连接信号
#============================================================
func _on_FoldButton_toggled(button_pressed):
	if button_pressed:
		show_panel()
	else:
		hide_panel()

func _on_Dialog_popup_hide():
	pressed = false

